package org.j4me.deviceinfo.data;

import java.util.StringTokenizer;
import java.util.Vector;

import org.dom4j.Element;

public class MediaContent {
	private String _mimeType;
	private Vector<String> _protocols = new Vector<String>();

	public MediaContent(String mimeType, String protocols) {
		_mimeType = mimeType;
		StringTokenizer words = new StringTokenizer(protocols, " {},");
		while (words.hasMoreTokens())
			_protocols.add(words.nextToken());
	}

	public MediaContent(Element elt) {
		_mimeType = elt.attributeValue("type");
		StringTokenizer words = new StringTokenizer(elt.attributeValue("protocols"), ",");
		while (words.hasMoreTokens())
			_protocols.add(words.nextToken());
	}

	public void toXml(Element elt) {
		String protocols = "";
		for (int i = 0; i < _protocols.size(); i++) {
			if (i == 0)
				protocols += _protocols.elementAt(i);
			else
				protocols += "," + _protocols.elementAt(i);
		}
		elt.addAttribute("type", _mimeType);
		elt.addAttribute("protocols", protocols);
	}

	public String getMimeType() {
		return _mimeType;
	}

	public void setMimeType(String mimeType) {
		_mimeType = mimeType;
	}

	public Vector<String> getProtocols() {
		return _protocols;
	}

	public void setProtocols(Vector<String> protocols) {
		_protocols = protocols;
	}


	
	public boolean equals(Object obj) {
		if (obj instanceof MediaContent) {
			MediaContent content = (MediaContent) obj;
			if( (_mimeType == content._mimeType || (_mimeType != null && _mimeType.equals(content._mimeType))) && _protocols.size() == content._protocols.size() ) {
				for(int i = 0; i < _protocols.size(); i++)
				{
					if(!content._protocols.contains(_protocols.elementAt(i)))
						return false;
				}
				return true;
			}
			return false;
		}
		return super.equals(obj);
	}
	
	
}
